# -*- coding: utf-8 -*-

import smtplib  # 导入发送邮件模块
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase  # 导入构建邮件模块：MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText  # 纯文本邮件
from email.utils import parseaddr, formataddr
from app.core.config import Config
from app.core.log import Log


class SendMail():
    def __init__(self):
        # 加载配置文件
        self.config = Config.init()
        self.config.read('../../config/mail.ini')
        # 邮件信息赋值
        self.mail_send = self.config.get("mail","mail_send")
        self.mail_passwd = self.config.get("mail","mail_passwd")
        self.mail_sever = self.config.get("mail","mail_sever")
        self.receiver = self.config.get("mail","receiver")
        self.mail_port = self.config.get("mail","mail_port")
        self.filepath = self.config.get("mail","filepath")


        # self.text = None
        # self.subtype = None

    def _format_addr(self, value):
        name, addr = parseaddr(value)
        return formataddr((Header(name, "utf8").encode(), addr))

    def choice_subtype(self, subtype = None, text = None):
        if subtype == "plain" and text != None:
            self.text = text
            self.subtype = "plain"
            return self.send_text_mail(), self.subtype, self.text

        elif subtype == "html" and text != None:
            self.text = text
            self.subtype = "html"
            return self.send_html_mail(), self.subtype, self.text

        elif subtype == None and text != None or text == None:
            self.text = text
            return self.send_attach_mail(), self.text

    def send_email(self, message):
        try:
            sever = smtplib.SMTP(self.mail_sever, self.mail_port)
            sever.set_debuglevel(1)  # 打印出SMTP服务器交互信息
            sever.login(self.mail_send, self.mail_passwd)
            sever.sendmail(self.mail_send, self.receiver, message.as_string())
            sever.quit()
            print("邮件发送成功！")
        except Exception as e:
            Log.init(e)
            print("发送邮件失败！")

    def send_text_mail(self):
        text_message = MIMEText(_text=self.text, _subtype=self.subtype, _charset="utf8")
        text_message['From'] = self._format_addr("<%s>" % self.mail_send)  # 显示发件人
        text_message['to'] = self._format_addr("<%s>" % self.receiver)  # 显示收件人
        text_message['subject'] = Header("接口测试报告", "utf-8").encode()  # 显示主题
        self.send_email(text_message)

    def send_html_mail(self):
        html_message = MIMEText(_text=self.text, _subtype=self.subtype, _charset="utf8")
        html_message['From'] = self._format_addr("<%s>" % self.mail_send)
        html_message['to'] = self._format_addr("<%s>" % self.receiver)
        html_message['subject'] = Header("接口测试报告", "utf-8").encode()
        self.send_email(html_message)

    def send_attach_mail(self):
        msg = MIMEMultipart()
        msg['From'] = self._format_addr("<%s>" % self.mail_send)
        msg['to'] = self._format_addr("<%s>" % self.receiver)
        msg['subject'] = Header("接口测试报告", "utf-8").encode()

        msg.attach(MIMEText(_text=self.text, _charset="utf8"))

        with open(Config().get_mail("filepath"), mode="rb") as file:
            mine = MIMEBase("PPT", "pptx", filename="测试课件.pptx")
            mine.add_header('Content-Disposition', 'attachment', filename='测试课件.pptx')  # 加入头部信息

            mine.set_payload(file.read())
            encoders.encode_base64(mine)
            msg.attach(mine)

        self.send_email(msg)


if __name__ == '__main__':
    text = "桃之夭夭，灼灼其华,我修改了数据"
    s = SendMail()

    s.choice_subtype()



